package com.open.common.utils;

import org.activiti.engine.impl.form.StringFormType;
import org.slf4j.Logger;	
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期处理
 */
public class DateUtils {
    private final static Logger logger = LoggerFactory.getLogger(DateUtils.class);
    /**
     * 时间格式(yyyy-MM-dd)
     */
    
    public final static String DATE_MM = "yyyy-MM";
    
    public final static String DATE_PATTERN = "yyyy-MM-dd";
    
    public static final String FORMAT_YYYY_MM_DD = "yyyy-MM-dd";
    /**
     * 时间格式(yyyy-MM-dd HH:mm:ss)
     */
    public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

    public static String format(Date date) {
        return format(date, DATE_PATTERN);
    }
    
    public static String formatTwo(Date date) {
        return format(date, DATE_MM);
    }

    
    
    
    
    public static String format(Date date, String pattern) {
        if (date != null) {
            SimpleDateFormat df = new SimpleDateFormat(pattern);
            return df.format(date);
        }
        return null;
    }

    /**
     * 计算距离现在多久，非精确
     *
     * @param date
     * @return
     */
    public static String getTimeBefore(Date date) {
        Date now = new Date();
        long l = now.getTime() - date.getTime();
        long day = l / (24 * 60 * 60 * 1000);
        long hour = (l / (60 * 60 * 1000) - day * 24);
        long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
        long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        String r = "";
        if (day > 0) {
            r += day + "天";
        } else if (hour > 0) {
            r += hour + "小时";
        } else if (min > 0) {
            r += min + "分";
        } else if (s > 0) {
            r += s + "秒";
        }
        r += "前";
        return r;
    }

    /**
     * 计算距离现在多久，精确
     *
     * @param date
     * @return
     */
    public static String getTimeBeforeAccurate(Date date) {
        Date now = new Date();
        long l = now.getTime() - date.getTime();
        long day = l / (24 * 60 * 60 * 1000);
        long hour = (l / (60 * 60 * 1000) - day * 24);
        long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
        long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        String r = "";
        if (day > 0) {
            r += day + "天";
        }
        if (hour > 0) {
            r += hour + "小时";
        }
        if (min > 0) {
            r += min + "分";
        }
        if (s > 0) {
            r += s + "秒";
        }
        r += "前";
        return r;
    }
    
    public static String getDateData(int a,Date date){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, a);
		return formatDate(cal.getTime(), FORMAT_YYYY_MM_DD);
	}
    
    
    public static String formatDate(Date date, String format) {
		if (date == null) {
			return null;
		}
		SimpleDateFormat objSDF = new SimpleDateFormat(format);
		String strOrderDate = objSDF.format(date);
		return strOrderDate;
	}
    
    
    /**  
     * 计算两个日期之间相差的天数  
     * @param smdate 较小的时间 
     * @param bdate  较大的时间 
     * @return 相差天数 
     * @throws ParseException  
     */    
    public static int daysBetween(Date smdate,String bdate1){    
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
        try {
			smdate=sdf.parse(sdf.format(smdate));
			Date bdate=sdf.parse(bdate1);  
			Calendar cal = Calendar.getInstance();    
	        cal.setTime(smdate);    
	        long time1 = cal.getTimeInMillis();                 
	        cal.setTime(bdate);    
	        long time2 = cal.getTimeInMillis();         
	        long between_days=(time2-time1)/(1000*3600*24);     
	        return Integer.parseInt(String.valueOf(between_days));     
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return 0;  
             
    } 
    
    /**
	 * 获取当前月第一天
	 * @return
	 */
	public static String get15Day(){
		Calendar cale = Calendar.getInstance();
		cale.set(Calendar.DAY_OF_MONTH,15);
		cale.add(Calendar.MONTH, 1);
		cale.set(Calendar.HOUR_OF_DAY,0);
		cale.set(Calendar.MINUTE,0);
		cale.set(Calendar.SECOND,0);
		return formatDate(cale.getTime(),"yyyy-MM-dd");
	}
    
    public static void main(String[] args) {
    /*	BigDecimal a=new BigDecimal("1.64345");
    	BigDecimal b=new BigDecimal("1.54456");
    	System.out.println(a.add(b).setScale(0, BigDecimal.ROUND_DOWN));*/
    	
    	
    	/*BigDecimal everyDayBen=new BigDecimal("10300.00").divide(new BigDecimal(18),2, BigDecimal.ROUND_DOWN);
		System.out.println("22");
    	everyDayBen=everyDayBen.divide(new BigDecimal(30)).setScale(2, BigDecimal.ROUND_DOWN);
		System.out.println(everyDayBen);*/
    	/*String date="2019-01-04";
    	int day=DateUtils.daysBetween(formatDate(cale.getTime(),"yyyy-MM-dd");,DateUtils.get15Day());
    	System.out.println(day);*/
    	
	}
}
